﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.RDLC

Partial Class DocProposal
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        If Not Page.IsPostBack Then

            CBEBack.ConfirmText = "You are about to navigate away from this page. " & vbCrLf & "Changes will be discard!"
            CBEDownload.ConfirmText = "All the changes will be saved before you download the document!"

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

            If Not Session("id") Is Nothing Then
                lblDocId.Text = Me.Session("id").ToString
                pnlEntry.Visible = True
                pnlPreview.Visible = False
                LoadEntry()
            Else
                pnlEntry.Visible = True
                pnlPreview.Visible = False
            End If
        End If
    End Sub

    Private Sub GetStaff(ByVal rep As Repeater)
        Dim TempDT As New Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT StaffId,Name FROM AW_Staff WHERE StatusDelete = 0 ORDER BY Name"
        objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        rep.DataSource = TempDT
        rep.DataBind()
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()
            Dim DocPropId As String
            If lblDocId.Text = String.Empty Then
                objDB.dbVariable.SQLString = "INSERT INTO AW_DocProposal(DocPropId, DocPropTitle, DocTo, ContactNo, Address, " & _
                                            "MailFax, REN, Attn, DocDate, MeetType, YourType, YourName, OurName, OnDate, " & _
                                            "CreateBy, CreateDate, UpdateDate, StatusDelete, ListType) VALUES (@DocPropId, @DocPropTitle, " & _
                                            "@DocTo, @ContactNo, @Address, @MailFax, @REN, @Attn, @DocDate, @MeetType, " & _
                                            "@YourType, @YourName, @OurName, @OnDate, @CreateBy, @CreateDate, @UpdateDate, @StatusDelete, @ListType)"
                DocPropId = Guid.NewGuid.ToString
                lblUpdated.Text = "Created By : " & GetName() & " on " & Format(Now(), "dd-MMM-yyyy")
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, DBNull.Value)
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, DBNull.Value)
            Else
                objDB.dbVariable.SQLString = "UPDATE AW_DocProposal SET DocPropTitle=@DocPropTitle, DocTo=@DocTo, ContactNo=@ContactNo, Address=@Address, " & _
                                        "MailFax=@MailFax, REN=@REN, Attn=@Attn, DocDate=@DocDate, MeetType=@MeetType, YourType=@YourType, " & _
                                        "YourName=@YourName, OurName=@OurName, OnDate=@OnDate, UpdateBy=@UpdateBy, UpdateDate=@UpdateDate, " & _
                                        "ListType=@ListType WHERE DocPropId=@DocPropId"
                DocPropId = lblDocId.Text
                lblUpdated.Text = "Last Updated By : " & GetName() & " on " & Format(Now(), "dd-MMM-yyyy")
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, Now())
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            End If
            objDB.AddParameter("DocPropId", Data.SqlDbType.UniqueIdentifier, New Guid(DocPropId))
            objDB.AddParameter("DocPropTitle", Data.SqlDbType.NVarChar, txtTitle.Text.Trim.ToUpper)
            If txtTo.Text = String.Empty Then
                objDB.AddParameter("DocTo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("DocTo", Data.SqlDbType.NVarChar, txtTo.Text.Trim)
            End If
            If txtContact.Text = String.Empty Then
                objDB.AddParameter("ContactNo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("ContactNo", Data.SqlDbType.NVarChar, txtContact.Text.Trim)
            End If
            If txtAddress.Text = String.Empty Then
                objDB.AddParameter("Address", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Address", Data.SqlDbType.NVarChar, txtAddress.Text.Trim)
            End If
            If ddlMailFax.SelectedValue = String.Empty Then
                objDB.AddParameter("MailFax", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("MailFax", Data.SqlDbType.NVarChar, ddlMailFax.SelectedValue)
            End If
            If txtREN.Text = String.Empty Then
                objDB.AddParameter("REN", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("REN", Data.SqlDbType.NVarChar, txtREN.Text.Trim)
            End If
            If txtAttn.Text = String.Empty Then
                objDB.AddParameter("Attn", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Attn", Data.SqlDbType.NVarChar, txtAttn.Text.Trim)
            End If
            If txtDate.Text = String.Empty Then
                objDB.AddParameter("DocDate", Data.SqlDbType.DateTime, DBNull.Value)
            Else
                objDB.AddParameter("DocDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtDate.Text.Trim, "dd-MMM-yyyy", Nothing))
            End If
            If ddlType.SelectedValue = String.Empty Then
                objDB.AddParameter("MeetType", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("MeetType", Data.SqlDbType.NVarChar, ddlType.SelectedValue)
            End If
            If ddlyour.SelectedValue = String.Empty Then
                objDB.AddParameter("YourType", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("YourType", Data.SqlDbType.NVarChar, ddlyour.SelectedValue)
            End If
            If txtName.Text = String.Empty Then
                objDB.AddParameter("YourName", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("YourName", Data.SqlDbType.NVarChar, txtName.Text.Trim)
            End If
            If txtOurName.Text = String.Empty Then
                objDB.AddParameter("OurName", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("OurName", Data.SqlDbType.NVarChar, txtOurName.Text.Trim)
            End If
            If txtOnDate.Text = String.Empty Then
                objDB.AddParameter("OnDate", Data.SqlDbType.DateTime, DBNull.Value)
            Else
                objDB.AddParameter("OnDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtOnDate.Text.Trim, "dd-MMM-yyyy", Nothing))
            End If
            objDB.AddParameter("ListType", Data.SqlDbType.NVarChar, ddlListType.SelectedValue)
            objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now())
            objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
            objDB.ExecuteQuery()
            objDB.ClearParameter()
            InsertProperty(objDB, New Guid(DocPropId))
            objDB.DbTransCommit()
            objDB.CloseConnection()
            lblDocId.Text = DocPropId
            lblMsgSuccessful.Text = "Document Saved"
            divSuccessful.Visible = True
        Catch ex As Exception
            objDB.DbTransRollback()
            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.CloseConnection()
            End If
            lblMsgError.Text = ex.Message
            divError.Visible = True
        End Try
    End Sub

    Private Sub InsertProperty(ByVal objDB As Database, ByVal DocPropId As Guid)
        Dim myDT As New Data.DataTable
        myDT = CType(ViewState("DataTable"), Data.DataTable)
        If Not myDT Is Nothing Then
            For Each myrow As Data.DataRow In myDT.Rows
                'If myrow.Item("Status") = "N" And myrow.Item("Visible") = "1" Then
                objDB.dbVariable.SQLString = "DELETE FROM AW_DocProposalProperty WHERE DocPropId=@DocPropId"
                objDB.AddParameter("DocPropId", Data.SqlDbType.UniqueIdentifier, DocPropId)
                objDB.ExecuteQuery()
                objDB.ClearParameter()
                If myrow.Item("Visible") = "1" Then
                    'insert
                    objDB.dbVariable.SQLString = "INSERT INTO AW_DocProposalProperty (DocPPId, DocPropId, Address, Location, " & _
                                            "Price, Tenure, Type, LA, BU, Remarks) VALUES (@DocPPId, @DocPropId, @Address, " & _
                                            "@Location, @Price, @Tenure, @Type, @LA, @BU, @Remarks)"
                    objDB.AddParameter("DocPPId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("DocPPId").ToString))
                    objDB.AddParameter("DocPropId", Data.SqlDbType.UniqueIdentifier, DocPropId)
                    If myrow.Item("Address").ToString = "" Then
                        objDB.AddParameter("Address", Data.SqlDbType.NVarChar, DBNull.Value)
                    Else
                        objDB.AddParameter("Address", Data.SqlDbType.NVarChar, myrow.Item("Address").ToString)
                    End If
                    If myrow.Item("Location").ToString = "" Then
                        objDB.AddParameter("Location", Data.SqlDbType.NVarChar, DBNull.Value)
                    Else
                        objDB.AddParameter("Location", Data.SqlDbType.NVarChar, myrow.Item("Location").ToString)
                    End If
                    If myrow.Item("Price").ToString = "" Then
                        objDB.AddParameter("Price", Data.SqlDbType.Decimal, DBNull.Value)
                    Else
                        objDB.AddParameter("Price", Data.SqlDbType.Decimal, CDbl(myrow.Item("Price").ToString))
                    End If
                    If myrow.Item("Tenure").ToString = "" Then
                        objDB.AddParameter("Tenure", Data.SqlDbType.NVarChar, DBNull.Value)
                    Else
                        objDB.AddParameter("Tenure", Data.SqlDbType.NVarChar, myrow.Item("Tenure").ToString)
                    End If
                    If myrow.Item("Type").ToString = "" Then
                        objDB.AddParameter("Type", Data.SqlDbType.NVarChar, DBNull.Value)
                    Else
                        objDB.AddParameter("Type", Data.SqlDbType.NVarChar, myrow.Item("Type").ToString)
                    End If
                    If myrow.Item("LA").ToString = "" Then
                        objDB.AddParameter("LA", Data.SqlDbType.Decimal, DBNull.Value)
                    Else
                        objDB.AddParameter("LA", Data.SqlDbType.Decimal, CDbl(myrow.Item("LA").ToString))
                    End If
                    If myrow.Item("BU").ToString = "" Then
                        objDB.AddParameter("BU", Data.SqlDbType.Decimal, DBNull.Value)
                    Else
                        objDB.AddParameter("BU", Data.SqlDbType.Decimal, CDbl(myrow.Item("BU").ToString))
                    End If
                    If txtRemarks.Text = "" Then
                        objDB.AddParameter("Remarks", Data.SqlDbType.NVarChar, DBNull.Value)
                    Else
                        objDB.AddParameter("Remarks", Data.SqlDbType.NVarChar, myrow.Item("Remarks").ToString)
                    End If
                    objDB.ExecuteQuery()
                    objDB.ClearParameter()
                    'Else
                    '    If myrow.Item("Visible") = "0" Then
                    '        'delete
                    '        objDB.dbVariable.SQLString = "DELETE AW_DocProposalProperty WHERE DocPPId=@DocPPId"
                    '        objDB.AddParameter("DocPPId", Data.SqlDbType.UniqueIdentifier, New Guid(myrow.Item("DocPPId").ToString))
                    '        objDB.ExecuteQuery()
                    '        objDB.ClearParameter()
                    '    End If
                End If
            Next
        End If
    End Sub

    Private Function GetName() As String
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Dim StaffName As String = String.Empty

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT Name FROM AW_Staff WHERE StaffId=@StaffId"
        objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
        objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        For Each dtrow As Data.DataRow In TempDT.Rows
            If dtrow.Item("Name").ToString <> "" Then
                StaffName = dtrow.Item("Name").ToString
            End If
        Next

        Return StaffName
    End Function

    Private Sub LoadEntry()

        Dim TempDT As New Data.DataTable
        TempDT = LoadDocProposal()
        For Each dtrow As Data.DataRow In TempDT.Rows
            txtTitle.Text = dtrow.Item("DocPropTitle").ToString
            txtTo.Text = dtrow.Item("DocTo").ToString
            txtContact.Text = dtrow.Item("ContactNo").ToString
            txtAddress.Text = dtrow.Item("Address").ToString
            ddlMailFax.SelectedValue = dtrow.Item("MailFax").ToString
            txtREN.Text = dtrow.Item("REN").ToString
            txtAttn.Text = dtrow.Item("Attn").ToString
            If dtrow.Item("DocDate").ToString = "" Then
                txtDate.Text = ""
            Else
                txtDate.Text = Format(dtrow.Item("DocDate"), "dd-MMM-yyyy")
            End If
            ddlType.SelectedValue = dtrow.Item("MeetType").ToString
            ddlyour.SelectedValue = dtrow.Item("YourType").ToString
            txtName.Text = dtrow.Item("YourName").ToString
            txtOurName.Text = dtrow.Item("OurName").ToString
            ddlListType.SelectedValue = dtrow.Item("ListType").ToString
            lblUpdated.Text = "Last Updated By : " & dtrow.Item("CreateBy").ToString & " on " & Format(dtrow.Item("CreateDate"), "dd-MMM-yyyy")
        Next

        TempDT.Clear()
        TempDT.Reset()

        TempDT = GetRepProperty()
        repPropertyAdded.DataSource = TempDT
        repPropertyAdded.DataBind()
        ViewState("DataTable") = TempDT
        VisibilityRepPropertyAdded()
    End Sub

    Private Sub AddNewRow()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT CASE p.Tenure WHEN '1' THEN 'FreeHold' WHEN '2' THEN 'ListHold' ELSE '' END as Tenure, " & _
                                    ddlListType.SelectedValue & "Amt as Price, t.Type, p.LASF, p.BUSF " & _
                                    "FROM AW_Property p LEFT JOIN AW_PropertyType t ON p.PropertyTypeId = t.TypeId " & _
                                    "WHERE p.PropertyId=@PropertyId"
        objDB.AddParameter("PropertyId", Data.SqlDbType.UniqueIdentifier, New Guid(lblPropertyId.Text))
        objDB.FillData(TempDT, "AW_Property", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        Dim MyDT As New Data.DataTable

        If ViewState("DataTable") Is Nothing Then
            DeclareColumn(MyDT)
            Dim MyRow As Data.DataRow
            MyRow = MyDT.NewRow()
            MyRow(0) = Guid.NewGuid
            MyRow(1) = txtPropertyAddress.Text.Trim
            MyRow(2) = lblLocation.Text.Trim
            MyRow(3) = CDbl(TempDT.Rows(0).Item("Price").ToString)
            MyRow(4) = TempDT.Rows(0).Item("Tenure").ToString
            MyRow(5) = TempDT.Rows(0).Item("Type").ToString
            MyRow(6) = TempDT.Rows(0).Item("LASF").ToString
            MyRow(7) = TempDT.Rows(0).Item("BUSF").ToString
            MyRow(8) = txtRemarks.Text.Trim
            MyRow(9) = "N"
            MyRow(10) = "1"
            MyDT.Rows.Add(MyRow)
        Else
            MyDT = CType(ViewState("DataTable"), Data.DataTable)
            Dim MyRow As Data.DataRow
            MyRow = MyDT.NewRow()
            MyRow(0) = Guid.NewGuid
            MyRow(1) = txtPropertyAddress.Text.Trim
            MyRow(2) = lblLocation.Text.Trim
            MyRow(3) = CDbl(TempDT.Rows(0).Item("Price").ToString)
            MyRow(4) = TempDT.Rows(0).Item("Tenure").ToString
            MyRow(5) = TempDT.Rows(0).Item("Type").ToString
            MyRow(6) = TempDT.Rows(0).Item("LASF").ToString
            MyRow(7) = TempDT.Rows(0).Item("BUSF").ToString
            MyRow(8) = txtRemarks.Text.Trim
            MyRow(9) = "N"
            MyRow(10) = "1"
            MyDT.Rows.Add(MyRow)
        End If
        ViewState("DataTable") = MyDT
        repPropertyAdded.DataSource = MyDT
        repPropertyAdded.DataBind()
        VisibilityRepPropertyAdded()
    End Sub

    Private Sub DeclareColumn(ByVal MyDT As Data.DataTable)
        Dim DocPPId As Data.DataColumn = New Data.DataColumn("DocPPId")
        DocPPId.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(DocPPId)
        Dim Address As Data.DataColumn = New Data.DataColumn("Address")
        Address.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Address)
        Dim Location As Data.DataColumn = New Data.DataColumn("Location")
        Location.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Location)
        Dim Price As Data.DataColumn = New Data.DataColumn("Price")
        Price.DataType = System.Type.GetType("System.Decimal")
        MyDT.Columns.Add(Price)
        Dim Tenure As Data.DataColumn = New Data.DataColumn("Tenure")
        Tenure.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Tenure)
        Dim Type As Data.DataColumn = New Data.DataColumn("Type")
        Type.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Type)
        Dim LA As Data.DataColumn = New Data.DataColumn("LA")
        LA.DataType = System.Type.GetType("System.Decimal")
        MyDT.Columns.Add(LA)
        Dim BU As Data.DataColumn = New Data.DataColumn("BU")
        BU.DataType = System.Type.GetType("System.Decimal")
        MyDT.Columns.Add(BU)
        Dim Remarks As Data.DataColumn = New Data.DataColumn("Remarks")
        Remarks.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Remarks)
        Dim Status As Data.DataColumn = New Data.DataColumn("Status")
        Status.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Status)
        Dim Visible As Data.DataColumn = New Data.DataColumn("Visible")
        Visible.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Visible)
    End Sub

    Private Function GetRepProperty() As Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT DocPPId, Address, Location, Price, Tenure, Type, LA, BU, " & _
                                    "Remarks, 'O' AS Status, '1' AS Visible FROM AW_DocProposalProperty WHERE " & _
                                    "DocPropId=@DocPropId"
        objDB.AddParameter("DocPropId", Data.SqlDbType.UniqueIdentifier, New Guid(lblDocId.Text))
        objDB.FillData(TempDT, "DocProposalProperty", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()
        Return TempDT
    End Function

    Private Sub VisibilityRepPropertyAdded()
        For Each repitem As RepeaterItem In repPropertyAdded.Items
            If CType(repitem.FindControl("lblhidRepVisible"), Label).Text = "0" Then
                repitem.Visible = False
            Else
                repitem.Visible = True
            End If
        Next
    End Sub

    Private Function LoadDocProposal() As Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT d.DocPropId, d.DocPropTitle, d.DocTo, d.ContactNo, d.Address, d.MailFax, d.REN, d.Attn, " & _
                                    "d.DocDate, d.MeetType, d.YourType, d.YourName, d.OurName, d.OnDate, ISNULL(d.UpdateDate, d.CreateDate) as CreateDate, " & _
                                    "ISNULL(u.Initial, c.Initial) as CreateBy, d.StatusDelete, d.ListType FROM AW_DocProposal d " & _
                                    "LEFT JOIN AW_Staff u ON d.UpdateBy = u.StaffId LEFT JOIN AW_Staff c ON " & _
                                    "d.CreateBy = c.StaffId WHERE DocPropId=@DocPropId"
        objDB.AddParameter("DocPropId", Data.SqlDbType.UniqueIdentifier, New Guid(lblDocId.Text))
        objDB.FillData(TempDT, "DocProposal", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()
        Return TempDT
    End Function

    Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        pnlEntry.Visible = False
        pnlPreview.Visible = True

        Dim TempDT As New Data.DataTable
        Dim PropertyDT As New Data.DataTable

        'TempDT = LoadDocProposal()
        DeclareColumnTempDT(TempDT)
        Dim MyRow As Data.DataRow
        MyRow = TempDT.NewRow()
        MyRow(0) = txtTitle.Text.Trim.ToUpper
        MyRow(1) = txtTo.Text.Trim
        MyRow(2) = txtContact.Text.Trim
        MyRow(3) = txtAddress.Text.Trim
        MyRow(4) = ddlMailFax.SelectedValue
        MyRow(5) = txtREN.Text.Trim
        MyRow(6) = txtAttn.Text.Trim
        If txtDate.Text = String.Empty Then
            MyRow(7) = DBNull.Value
        Else
            MyRow(7) = DateTime.ParseExact(txtDate.Text.Trim, "dd-MMM-yyyy", Nothing)
        End If
        MyRow(8) = ddlType.SelectedValue
        MyRow(9) = ddlyour.SelectedValue
        MyRow(10) = txtName.Text.Trim
        MyRow(11) = txtOurName.Text.Trim
        If txtOnDate.Text = String.Empty Then
            MyRow(12) = DBNull.Value
        Else
            MyRow(12) = DateTime.ParseExact(txtOnDate.Text.Trim, "dd-MMM-yyyy", Nothing)
        End If
        MyRow(13) = GetName()
        MyRow(14) = Now()
        TempDT.Rows.Add(MyRow)
        If ViewState("DataTable") Is Nothing Then
            PropertyDT = New Data.DataTable
        Else
            PropertyDT = ViewState("DataTable")
        End If
      
        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptDocProposal.rdlc", "DocProposal")
            TempDT.TableName = "dsDocProposal_AW_DocProposal"
            PropertyDT.TableName = "dsDocProposal_AW_DocProposalProperty"
            objRDLC.AddDatabase(TempDT)
            objRDLC.AddDatabase(PropertyDT)
            ReportViewer1.LocalReport.DataSources.Clear()
            Dim param(0) As Microsoft.Reporting.WebForms.ReportParameter
            param(0) = New Microsoft.Reporting.WebForms.ReportParameter("Category", ddlListType.SelectedValue, False)
            ReportViewer1.LocalReport.SetParameters(param)
            ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource(TempDT.TableName, TempDT))
            ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource(PropertyDT.TableName, PropertyDT))
            ReportViewer1.LocalReport.Refresh()
        End If
    End Sub

    Private Sub DeclareColumnTempDT(ByVal MyDT As Data.DataTable)
        Dim DocPropTitle As Data.DataColumn = New Data.DataColumn("DocPropTitle")
        DocPropTitle.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(DocPropTitle)
        Dim DocTo As Data.DataColumn = New Data.DataColumn("DocTo")
        DocTo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(DocTo)
        Dim ContactNo As Data.DataColumn = New Data.DataColumn("ContactNo")
        ContactNo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(ContactNo)
        Dim Address As Data.DataColumn = New Data.DataColumn("Address")
        Address.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Address)
        Dim MailFax As Data.DataColumn = New Data.DataColumn("MailFax")
        MailFax.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(MailFax)
        Dim REN As Data.DataColumn = New Data.DataColumn("REN")
        REN.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(REN)
        Dim Attn As Data.DataColumn = New Data.DataColumn("Attn")
        Attn.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Attn)
        Dim DocDate As Data.DataColumn = New Data.DataColumn("DocDate")
        DocDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(DocDate)
        Dim MeetType As Data.DataColumn = New Data.DataColumn("MeetType")
        MeetType.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(MeetType)
        Dim YourType As Data.DataColumn = New Data.DataColumn("YourType")
        YourType.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(YourType)
        Dim YourName As Data.DataColumn = New Data.DataColumn("YourName")
        YourName.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(YourName)
        Dim OurName As Data.DataColumn = New Data.DataColumn("OurName")
        OurName.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(OurName)
        Dim OnDate As Data.DataColumn = New Data.DataColumn("OnDate")
        OnDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(OnDate)
        Dim CreateBy As Data.DataColumn = New Data.DataColumn("CreateBy")
        CreateBy.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(CreateBy)
        Dim CreateDate As Data.DataColumn = New Data.DataColumn("CreateDate")
        CreateDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(CreateDate)
    End Sub

    Protected Sub btnBackEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackEntry.Click
        pnlEntry.Visible = True
        pnlPreview.Visible = False
    End Sub

    Protected Sub btnSearchREN_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearchREN.Click
        GetStaff(repREN)
        mpeRENEntry.Show()
    End Sub

    Protected Sub repREN_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repREN.ItemCommand
        If e.CommandName = "SelectType" Then
            Dim REN As String = CType(repREN.Items(e.CommandArgument).FindControl("lnkRepREN"), LinkButton).Text
            txtREN.Text = REN
        End If
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If Session("Back") Is Nothing Then
            Response.Redirect("Documentation.aspx")
        Else
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            Response.Redirect(arr(0).ToString)
        End If
    End Sub

    Protected Sub btnSearchProperty_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearchProperty.Click
        If ddlListType.SelectedValue = "" Then
            lblMsgError.Text = "Please select a List Type"
            divError.Visible = True
        Else
            mpePropertyEntry.Show()
        End If
    End Sub

    Protected Sub btnpopupSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnpopupSearch.Click
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

        Dim Location, Road, UnitNumber As String

        If txtpopupLocation.Text = "" Then
            Location = String.Empty
        Else
            Location = " AND l.Location LIKE @Location"
            objDB.AddParameter("Location", Data.SqlDbType.NVarChar, "%" & txtpopupLocation.Text.Trim & "%")
        End If

        If txtpopupRoad.Text = "" Then
            Road = String.Empty
        Else
            Road = " AND r.Road LIKE @Road"
            objDB.AddParameter("Road", Data.SqlDbType.NVarChar, "%" & txtpopupRoad.Text.Trim & "%")
        End If

        If txtpopupUnitNumber.Text = "" Then
            UnitNumber = String.Empty
        Else
            UnitNumber = " AND p.UnitNumber LIKE @UnitNumber"
            objDB.AddParameter("UnitNumber", Data.SqlDbType.NVarChar, "%" & txtpopupUnitNumber.Text.Trim & "%")
        End If

        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()

        If Left(ddlListType.SelectedValue, 1) = "S" Then
            objDB.dbVariable.SQLString = "SELECT p.PropertyId, p.UnitNumber + ', ' + r.Road + ', ' + l.Location + ', ' + s.State + ', ' + c.Country as 'Address', " & _
                                   "l.Location FROM AW_Property p LEFT JOIN AW_Road r ON p.RoadId=r.RoadId LEFT JOIN AW_Location l " & _
                                   "ON r.LocationId = l.LocationId LEFT JOIN AW_State s ON l.StateId = s.StateId LEFT JOIN " & _
                                   "AW_Country c ON s.CountryId = c.CountryId WHERE SRelistStatus=0 AND " & ddlListType.SelectedValue & "Yes=1 " & _
                                   Location & Road & UnitNumber
        Else
            objDB.dbVariable.SQLString = "SELECT p.PropertyId, p.UnitNumber + ', ' + r.Road + ', ' + l.Location + ', ' + s.State + ', ' + c.Country as 'Address', " & _
                                   "l.Location FROM AW_Property p LEFT JOIN AW_Road r ON p.RoadId=r.RoadId LEFT JOIN AW_Location l " & _
                                   "ON r.LocationId = l.LocationId LEFT JOIN AW_State s ON l.StateId = s.StateId LEFT JOIN " & _
                                   "AW_Country c ON s.CountryId = c.CountryId WHERE RRelistStatus=0 AND " & ddlListType.SelectedValue & "Yes=1 " & _
                                   Location & Road & UnitNumber
        End If
       
        'objDB.AddParameter("DocPropId", Data.SqlDbType.UniqueIdentifier, New Guid(lblDocId.Text))
        objDB.FillData(TempDT, "DocProposalProperty", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        repProperty.DataSource = TempDT
        repProperty.DataBind()

        mpePropertyEntry.Show()
    End Sub

    Protected Sub repProperty_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repProperty.ItemCommand
        If e.CommandName.ToUpper = "SELECTTYPE" Then
            Dim Address As String = CType(repProperty.Items(e.CommandArgument).FindControl("lnkRepAddress"), LinkButton).Text
            Dim PropertyID As String = CType(repProperty.Items(e.CommandArgument).FindControl("lblRepPropertyId"), Label).Text
            Dim Location As String = CType(repProperty.Items(e.CommandArgument).FindControl("lblRepLocation"), Label).Text
            txtPropertyAddress.Text = Address
            lblPropertyId.Text = PropertyID
            lblLocation.Text = Location
        End If
    End Sub

    Protected Sub btnAddMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMore.Click
        If txtPropertyAddress.Text.Trim = "" Then
            lblMsgError.Text = "Please select a Property"
            divError.Visible = True
        Else
            AddNewRow()
            txtPropertyAddress.Text = ""
            lblPropertyId.Text = ""
            lblLocation.Text = ""
            txtRemarks.Text = ""
        End If

    End Sub

    Protected Sub repPropertyAdded_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repPropertyAdded.ItemCommand
        If e.CommandName.ToUpper = "MINUS" Then
            Dim MyDT As New Data.DataTable

            MyDT = CType(ViewState("DataTable"), Data.DataTable)

            MyDT.Rows(e.CommandArgument)(0) = MyDT.Rows(e.CommandArgument)(0).ToString
            MyDT.Rows(e.CommandArgument)(1) = MyDT.Rows(e.CommandArgument)(1).ToString
            MyDT.Rows(e.CommandArgument)(2) = MyDT.Rows(e.CommandArgument)(2).ToString
            MyDT.Rows(e.CommandArgument)(3) = MyDT.Rows(e.CommandArgument)(3).ToString
            MyDT.Rows(e.CommandArgument)(4) = MyDT.Rows(e.CommandArgument)(4).ToString
            MyDT.Rows(e.CommandArgument)(5) = MyDT.Rows(e.CommandArgument)(5).ToString
            MyDT.Rows(e.CommandArgument)(6) = MyDT.Rows(e.CommandArgument)(6).ToString
            MyDT.Rows(e.CommandArgument)(7) = MyDT.Rows(e.CommandArgument)(7).ToString
            MyDT.Rows(e.CommandArgument)(8) = MyDT.Rows(e.CommandArgument)(8).ToString
            MyDT.Rows(e.CommandArgument)(9) = MyDT.Rows(e.CommandArgument)(9).ToString
            MyDT.Rows(e.CommandArgument)(10) = "0"
            'Next
            MyDT.AcceptChanges()
            'End If
            ViewState("DataTable") = MyDT
            repPropertyAdded.DataSource = MyDT
            repPropertyAdded.DataBind()
            VisibilityRepPropertyAdded()
        End If
    End Sub

    Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Dim TempDT As New Data.DataTable
        Dim PropertyDT As New Data.DataTable

        BtnSave_Click(sender, e)

        Dim CurrentUser As String = GetName()
        Dim PrintStr As String = "Printed by " & CurrentUser & " on " & Format(Now(), "dd-MMM-yyyy")

        TempDT = LoadDocProposal()
        PropertyDT = GetRepProperty()
        'CommissionDT = LoadCommission()

        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptDocProposal.rdlc", "DocProposal")
            TempDT.TableName = "dsDocProposal_AW_DocProposal"
            PropertyDT.TableName = "dsDocProposal_AW_DocProposalProperty"
            objRDLC.AddParameter("Category", ddlListType.SelectedValue)
            objRDLC.AddParameter("PrintStr", PrintStr)
            objRDLC.AddDatabase(TempDT)
            objRDLC.AddDatabase(PropertyDT)
            objRDLC.ExportReport(RDLC.ExportTypeFormat.PDF, HttpContext.Current)
        End If
    End Sub
End Class
